<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Field extends MY_Admin {

    protected $data;

    function __construct () {
        parent::__construct();

        $this->load->model(array('category_model','field_model'));
        $this->load->library('MY_FormParam');
        
        $arrCategory = $this->category_model->getCategoryList();
        $this->data['category'] = MY_Helper::convArray($arrCategory, 'category_id', 'category_name');
    }

    function index(){
        $objFormParam = $this->my_formparam;
        $this->initSearchParam($objFormParam);
        
        $objFormParam->setParam($_POST);
        $objFormParam->trimParam();
        
        $this->data['arrForm']   = $objFormParam->getFormParamList();
        $this->data['arrHidden'] = $objFormParam->getSearchArray();
        
        $mode = $this->input->post('mode');
        switch ($mode){
            case "delete":
                $field_id = $this->input->post('field_id');
                if(!$this->delete('field',array('field_id' => $field_id))) {
                    $this->data["msg"] = "削除が失敗しました。";
                } else {
                    $this->data["msg"] = "削除が完了しました。";
                }
            case "search":
            default:
                $params = $objFormParam->getHashArray();
                $pager = array(
                        "cur_page"    => $this->input->post('search_pageno'),
                        "total_rows"  => count($this->field_model->getFieldList($params)),
                );
                $this->setPager($pager);
                $per_page = $this->pager['per_page'];
                $offset   = $this->offset();

                $this->data['field']     = $this->field_model->getFieldList($params, true, $per_page, $offset);
                $this->data['field_cnt'] = $this->pager['total_rows'];
                $this->data['pager'] 	 = $this->getPagerTpl();
                break;
        }
        $this->view('admin/field_list', $this->data);
    }

    function edit() {
        $objFormParam = $this->my_formparam;
        $this->initSearchParam($objFormParam);
        $this->initFormParam($objFormParam);
        
        $objFormParam->setParam($_POST);
        $objFormParam->trimParam();
        
        $this->data['arrHidden'] = $objFormParam->getSearchArray();
        $this->data['arrForm']   = $objFormParam->getFormParamList();
        
        $mode = $this->input->post('mode');
        switch ($mode)
        {
            case "pre_edit":
                $field_id = $this->input->post('field_id');
                $arrData = $this->field_model->getFieldById($field_id);
                $objFormParam->setParam($arrData);

                $this->data['arrForm']  = $objFormParam->getFormParamList();
                $this->data["field_id"] = $field_id;

                $template = 'admin/field_edit';
                break;
            case "complete":
                $validate = $this->field_model->exec_validation('field_edit');
                if($validate['status'] == FALSE){
                    $this->data['arrErr']  = $validate['data'];
                    return $this->view('admin/field_edit',$this->data);
                }

                $params = $objFormParam->getDbArray();
                if(!$this->editField($params)) {
                    $this->data["msg"] = "登録が失敗しました。";
                    return $this->view('admin/job_edit',$this->data);
                }

                $this->data["msg"] = "登録が完了致しました。";
		        $this->data['arrForm'] = null;
                $template          = 'admin/field_complete';
                break;
            default:
                $template = 'admin/field_edit';
                break;
        }
        $this->view($template, $this->data);
    }

    public function initFormParam(&$objFormParam)
    {
        $objFormParam->addParam('フィールドID', 'field_id');
        $objFormParam->addParam('フィールド名', 'field_name');
        $objFormParam->addParam('カテゴリ', 'category_id');
    }
    
    public function initSearchParam(&$objFormParam)
    {
        $objFormParam->addParam('カテゴリ', 'search_category_id','',false);
        $objFormParam->addParam('PageMax', 'search_page_max','',false);
        $objFormParam->addParam('PageNo', 'search_pageno','',false);
        $objFormParam->addParam('Display Search Form', 'search_form_display','',false);
    }
    
    public function editField($params = array())
    {
        if (empty($params)){
            return false;
        }
        $field_id = $params['field_id'];
        unset($params['field_id']);
        if(is_numeric($field_id)){
            if(!$this->update('field', $params, array('field_id' => $field_id))) {
                return false;
            }
        } else {
            if(!$this->insert('field', $params)) {
                return false;
            }
        }
        return true;
    }
}
